package com.chaoyong.higo.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import com.chaoyong.higo.R;
import com.chaoyong.higo.adapter.OrderManagerAdp;
import com.chaoyong.higo.base.BaseActivity;
import com.chaoyong.higo.bean.KuaiDiBean;
import com.chaoyong.higo.bean.ShowOrderEntity;
import com.chaoyong.higo.bean.ShowOrderManageBean;
import com.chaoyong.higo.bean.TestNewsetBean;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.EmptyUtil;
import com.chaoyong.higo.utils.JLog;
import com.chaoyong.higo.utils.PrefUtils;
import com.chaoyong.higo.utils.V;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 晒单管理
 * 
 * @author zk
 *
 */
public class OrderManagerActivity extends BaseActivity implements OnItemClickListener {

	private static final String ClassName = OrderManagerActivity.class.getName();

	private LinearLayout base_left_iv;// 左侧返回按钮
	private TextView base_title_tv;// 标题
	private TextView base_right_tv;// 标题右侧文字

	private ListView order_list_view;
	private OrderManagerAdp adapter;
	
	private List<ShowOrderManageBean.DataEntity.ListEntity> entities;

	@Override
	public int layoutResID() {
		return R.layout.activity_order_manager;
	}

	@Override
	public void findView() {
		findTitleIds();

		order_list_view = V.f(this, R.id.order_list_view);
	}

	private void findTitleIds() {
		base_left_iv = V.f(this, R.id.base_left_iv);
		base_title_tv = V.f(this, R.id.base_title_tv);
		base_right_tv = V.f(this, R.id.base_right_tv);

		base_title_tv.setText("晒单记录");
		base_right_tv.setVisibility(View.GONE);

	}

	@Override
	public void init() {
		base_left_iv.setOnClickListener(this);

		adapter = new OrderManagerAdp(this);
		order_list_view.setAdapter(adapter);
		order_list_view.setOnItemClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.base_left_iv:// title左侧返回
			this.finish();
			break;
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		String sh_uid = PrefUtils.getString(OrderManagerActivity.this, "userId", "1");
		httpShowOrderManager(sh_uid);
//		 httpShowOrderManager("1");
	}

	/**
	 * 晒单管理
	 * 
	 * @param sh_uid
	 */
	private void httpShowOrderManager(String sh_uid) {
		HttpUtils utils = new HttpUtils();
		RequestParams param = new RequestParams();
		try {
			JSONObject object = new JSONObject();

			JSONObject info = new JSONObject();
			info.putOpt("sh.uid", sh_uid);

			object.putOpt("model", "Share");
			object.putOpt("function", "getAllShares");
			object.putOpt("code", "1000");
			object.putOpt("info", info);

			param.setBodyEntity(new StringEntity(object.toString(), "UTF-8"));
		} catch (Exception e1) {
			e1.printStackTrace();/*  */
		}
		utils.send(HttpMethod.POST, OApi.Appport_Members, param,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						JLog.e(ClassName, arg0.getMessage());
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
 						String result = arg0.result;
						
						try {
							JSONObject jsonObject = new JSONObject(result);
							Gson gson = new Gson();
							String status = jsonObject.getString("status");

							if (status.equals("1")) {
								ShowOrderManageBean bean = gson.fromJson(result, ShowOrderManageBean.class);
								entities = bean.getData().getList();
								if (!EmptyUtil.isEmpty(entities)) {
									// int ind = entities.size();
									// showToast(ind + "");
									adapter.setList(entities);
//									adapter.addAllDataAndNorify(entities);
								}
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}

					}
				});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		ShowOrderEntity entity = new ShowOrderEntity();
		entity.setCover_img(entities.get(position).getCover_img().get(0));
		entity.setExpect(entities.get(position).getExpect());
		entity.setGoods_expect(entities.get(position).getGoods_expect());
		entity.setGoods_id(entities.get(position).getGoods_id());
		entity.setGoods_name(entities.get(position).getGoods_name());
		entity.setGoods_price(entities.get(position).getGoods_price());
		entity.setId(entities.get(position).getId());
		entity.setShare_info(entities.get(position).getShare_info());
		entity.setShare_time(entities.get(position).getShare_time());
		entity.setShare_title(entities.get(position).getShare_title());
		entity.setUser_name(PrefUtils.getString(this, "dear_name", "岳云鹏"));
		entity.setUid_portrait(entities.get(position).getCover_img().get(0));
		entity.setRecord(entities.get(position).getRecord());
		entity.setUid_portrait(entities.get(position).getUid_portrait());
		Intent in = new Intent(this, SharDetailActivity.class);
		in.putExtra("sharEntity", entity);
		this.startActivity(in);
	}

}
